<?php

namespace app\controller\admin\cnf;

use app\bll\ComponentEvent as BllComponentEvent;
use app\bll\ComponentEventMethod as BllComponentEventMethod;
use app\bll\CrontabTask as BllCrontabTask;
use core\controller\AdminBase;
use Exception;
use support\View;

class CrontabTask extends AdminBase
{
    public function index()
    {
        $bllCrontabTask = new BllCrontabTask();
        $tasks = $bllCrontabTask->paginator(
            title: request()->get('title', '')
        );
        View::assign('tasks', $tasks);
        return $this->view();
    }

    /**
     * 新增定时任务的方法
    
     * @return mixed 如果是POST请求，根据操作结果返回成功或错误信息；如果是GET请求，返回视图页面。
     */
    public function add()
    {
        // 检查当前请求是否为POST请求
        if (request()->isPost()) {
            try {
                // 创建BllCrontabTask类的实例，用于添加定时任务
                $bllCrontabTask = new BllCrontabTask();
                $bllCrontabTask->add(request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            // 创建BllComponentEvent类的实例，用于获取级联事件数据
            $bllComponentEvent            = new BllComponentEvent();
            // 调用 cascader 方法获取级联事件数据
            $events = $bllComponentEvent->cascader();

            // 将获取到的级联事件数据赋值给视图变量 'events'
            View::assign('events', $events);
            // 返回添加定时任务的视图页面
            return $this->view();
        }
    }

    public function edit()
    {
        $bllCrontabTask = new BllCrontabTask();
        $id = request()->get('id');
        if (request()->isPost()) {
            try {
                $bllCrontabTask->edit($id, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            // 创建BllComponentEvent类的实例，用于获取级联事件数据
            $bllComponentEvent            = new BllComponentEvent();
            // 调用 cascader 方法获取级联事件数据
            $events     = $bllComponentEvent->cascader();
            $task       = $bllCrontabTask->find($id);
            $eventinfo  = [];
            $methodinfo = [];

            if ($task['tasktype'] == 1) {
                //获取当前任务调用的是哪个事件
                list($event, $class, $method) = explode('/', $task['event']);
                $bllcomponentEventMethod      = new BllComponentEventMethod();
                $eventinfo                    = $bllComponentEvent->findByIdent($event);
                $methodinfo                   = $bllcomponentEventMethod->findByPath($class . '/' . $method, $eventinfo['id']);
            }
            View::assign('events', $events);
            View::assign('task', $task);
            View::assign('methodinfo', $methodinfo);
            View::assign('eventinfo', $eventinfo);


            return $this->view();
        }
    }

    public function setStatus()
    {
        try {
            $bllCrontabTask = new BllCrontabTask();
            $id             = request()->post('id');
            $status         = request()->post('status');
            $bllCrontabTask->setStatus($id, $status);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }

    public function delete()
    {
        try {
            $bllCrontabTask = new BllCrontabTask();
            $id             = request()->get('id');
            $bllCrontabTask->delete($id);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
